package view.microgui
{
	import control.micro.HumanBattleController;
	import events.BattleEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import model.macro.Unit;
	import model.micro.Battle;
	import model.micro.FieldUnit;
	
	/**
	 *
	 */
	public class HUDSprite extends Sprite
	{
		private var battle:Battle;
		private var garrison_unit_list:FieldUnitList;
		private var field_unit_list:FieldUnitList;
		private var toggle_button:Sprite;
		
		public function HUDSprite(battle:Battle)
		{
			this.battle = battle;
			
			battle.addEventListener(BattleEvent.GARRISON_DEPLOYED, garrisonDeployed);
			battle.addEventListener(BattleEvent.UNIT_GARRISONED, unitGarrisoned);
			battle.addEventListener(BattleEvent.UNIT_KILLED, unitKilled);
			
			garrison_unit_list = new FieldUnitList(battle.garrison);
			field_unit_list = new FieldUnitList();
			for each (var unit:FieldUnit in battle.defense_field_units)
			{
				field_unit_list.addUnit(unit.macroUnit);
			}
			
			field_unit_list.x = 10;
			field_unit_list.y = 20;
			garrison_unit_list.x = 10;
			garrison_unit_list.y = 20;
			
			field_unit_list.visible = false;
			
			addChild(garrison_unit_list);
			addChild(field_unit_list);
			
			toggle_button = new Sprite();
			toggle_button.graphics.beginFill(0x777777);
			toggle_button.graphics.drawRect(0, 0, 16, 8);
			toggle_button.graphics.endFill();
			toggle_button.x = 10;
			toggle_button.y = 10;
			toggle_button.addEventListener(MouseEvent.CLICK, function(e:Event):void
				{
					garrison_unit_list.toggleVisible();
					field_unit_list.toggleVisible();
				});
			addChild(toggle_button);
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
		
			// Measuring tools
			//graphics.lineStyle(1, 0x000000);
			//graphics.moveTo(stage.stageWidth / 2, 0);
			//graphics.lineTo(stage.stageWidth / 2, stage.stageHeight);
			//graphics.moveTo(0, stage.stageHeight / 2);
			//graphics.lineTo(stage.stageWidth, stage.stageHeight / 2);
		}
		
		private function unitKilled(e:BattleEvent):void
		{
			garrison_unit_list.removeUnit((e.data as FieldUnit).macroUnit);
			field_unit_list.removeUnit((e.data as FieldUnit).macroUnit);
		}
		
		private function unitGarrisoned(e:BattleEvent):void
		{
			field_unit_list.removeUnit((e.data as FieldUnit).macroUnit);
			garrison_unit_list.addUnit((e.data as FieldUnit).macroUnit);
		}
		
		private function garrisonDeployed(e:BattleEvent):void
		{
			garrison_unit_list.removeUnit((e.data as FieldUnit).macroUnit);
			field_unit_list.addUnit((e.data as FieldUnit).macroUnit);
		}
		
		public function update():void
		{
			garrison_unit_list.update();
			field_unit_list.update();
		}
	}
}
